﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.Configuration;
using System.IO;
using System.Text;
/// <summary>
/// Summary description for CSV
/// </summary>
/// 
public class CSV : Connector
{
    string Path;
    StreamReader reader;
    List<dynamic> Students;
    public CSV()
    {
        Path = ConfigurationManager.AppSettings["FilePath"];
        GetSettingsFromConfig();
        GetPropertiesFromConfig();
        GetConnect();
    }

    public override dynamic GetConnect()
    {
        reader = new StreamReader(File.OpenRead(Path), Encoding.UTF8);
        return reader;
    }
    public override List<dynamic> GetUsers()
    {
        string[] allstr = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        Students = new List<dynamic>();
        foreach (string str in allstr)
        {
            var values = str.Split(';');
            if (values.Count() != SettingsProperties.Count())
            {
                continue;
            }
            List<CatalogProperty> Student = new List<CatalogProperty>();
            for (int i = 0; i < SettingsProperties.Count(); i++)
            {
                CatalogProperty TempProperty = new CatalogProperty();
                TempProperty.Property = SettingsProperties[i];
                TempProperty.Value = values[i];
                Student.Add(TempProperty);
            }
            Students.Add(Student);
        }
        return Students;
    }
    public override void ChangeUserProperty(dynamic User, CatalogProperty[] Props)
    {

    }
    public override List<dynamic> FindUsers(CatalogProperty[] Props, bool OneEnough)
    {
        return null;
    }
    public string ShowPassword(List<CatalogProperty> Student)
    {
         foreach(CatalogProperty Prp in Student)
         {
             if(Prp.Property.ToLower()=="password" || Prp.Property.ToLower()=="pass")
             {
                 return Prp.Value;
             }
         }
         return null;
    }
    public override dynamic FindUser(CatalogProperty[] Props, bool OneEnough)
    {
        foreach (List<CatalogProperty> Student in Students)
        {
            int Count = 0;
            foreach (CatalogProperty PrpStudent in Student)
            {
                bool isMatch = !OneEnough;
                foreach (CatalogProperty Prp in Props)
                {
                    if(PrpStudent.Property==Prp.Property)
                    {
                        if(PrpStudent.Value==Prp.Value)
                        {
                            isMatch = true;
                            break;
                        }
                        else
                        {
                            isMatch = false;
                            break;
                        }
                    } 
                }
                Count = Count + Convert.ToInt32(isMatch);
            }
            if(Count==Props.Count())
            {
                return Student;
            }
        }

        return null;
    }
    public override string CreateUser(CatalogProperty[] Props)
    {
        return "NotImplemented";
    }
    public override bool ChangePassword(dynamic User, string Password)
    {
        return true;
    }
}

